package com.shenwei.yingdaoguanli.controller;

import com.shenwei.yingdaoguanli.service.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @Author wang
 * @Date 2025/2/18 11:05
 * @PackageName:com.shenwei.yingdaoguanli.controller
 * @ClassName: EmailController
 * @Description: TODO
 * @Version 1.0
 */

import java.io.File;

@RestController
public class EmailController {

    @Autowired
    private EmailService emailService;

    @PostMapping("/sendTestEmail")
    public String sendTestEmail() {
        String filePath = "D:/BI本地数据/data.zip";  // 文件路径
        String toEmail = "me6662021@163.com";
        String subject = "测试邮件";
        String body = "这是一个测试邮件，附件包含.zip文件。";

        boolean isSent = emailService.sendEmailWithAttachment(toEmail, subject, body, filePath);

        if (isSent) {
            // 如果邮件发送成功，删除文件
            File file = new File(filePath);
            if (file.exists()) {
                boolean isDeleted = file.delete();
                if (isDeleted) {
                    return "邮件发送成功，文件已删除！";
                } else {
                    return "邮件发送成功，但文件删除失败！";
                }
            }
            return "邮件发送成功，但文件不存在！";
        } else {
            return "发送邮件失败！";
        }
    }
}
